package org.meb.speedway.web;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;

import lombok.Getter;

import org.meb.speedway.bean.EventTaskQuery;
import org.meb.speedway.bean.ImportService;
import org.meb.speedway.bean.SearchService;
import org.meb.speedway.model.common.IntEvent;
import org.meb.speedway.web.log.Log;
import org.meb.speedway.web.log.LogStyleUtil;

@Named
@ConversationScoped
public class EventTasksBean implements Serializable {

	private static final long serialVersionUID = 2681020715995957490L;

	@Inject
	private SearchService searchService;
	@Inject
	private ImportService importService;
	@Inject
	private Conversation conversation;
	@Getter
	private List<IntEvent> eventTasks;
	@Getter
	private Log log;

	public EventTasksBean() {

	}

	@SuppressWarnings("unused")
	@PostConstruct
	private void initialize() {
		EventTaskQuery query = new EventTaskQuery();
		query.getSortOrder().descTrunc("eventDate").asc("eventGroup").asc("eventDate")
				.asc("eventName");
		eventTasks = searchService.find(query);
	}

	public void importEventTasks() {
		String logText = importService.importEventSources();
		log = new Log(logText, "\n", "\\|");
		LogStyleUtil.styleAsImportLog(log);
		// if (!conversation.isTransient()) {
		// conversation.end();
		// }
	}

	public void startConversation() {
		if (conversation.isTransient()) {
			conversation.begin();
		}
	}
}
